﻿
using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;
using Microsoft.Win32;
namespace taskmgr
{
	/// <summary>
	/// Description of SystemInfo.
	/// </summary>
	public class SystemInfo
	{
		[StructLayout(LayoutKind.Sequential)]
    	public struct SHELLEXECUTEINFO
    	{
        	public int cbSize;
        	public int fMask;
        	public IntPtr hwnd;
        	[MarshalAs(UnmanagedType.LPStr)]
        	public string lpVerb;
        	[MarshalAs(UnmanagedType.LPStr)]
        	public string lpFile;
        	[MarshalAs(UnmanagedType.LPStr)]
        	public string lpParameters;
        	[MarshalAs(UnmanagedType.LPStr)]
        	public string lpDirectory;
        	public int nShow;
        	public IntPtr hInstApp;
        	public IntPtr lpIDList;
        	[MarshalAs(UnmanagedType.LPStr)]
        	public string lpClass;
        	public IntPtr hkeyClass;
        	public int dwHotKey;
        	public IntPtr hIcon;
        	public IntPtr hProcess;
    	}
    	private const int SW_SHOW = 5;
		private const int SEE_MASK_INVOKEIDLIST =0x0000000C;
		private const int SEE_MASK_NOCLOSEPROCESS=0x00000040;
		private const int SEE_MASK_FLAG_NO_UI=0x00000400;
		[DllImport("shell32.dll")]
        static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo); 
        //====================================
        public const int EWX_LOGOFF=0;
        public const int EWX_SHUTDOWN=1;
        public const int EXW_REBOOT=2;
        public const int EXW_FORCE=4;
		[DllImport("user32.dll")]
        static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
        [DllImport("ntdll")]
		private static extern void NtShutdownSystem(int ShutdownAction);
		
        //[DllImport("kernel32.dll", SetLastError = true)]
		//[return: MarshalAs(UnmanagedType.Bool)]
		//static extern bool DeleteFile(string lpFileName);
		/// <summary>
		/// 删除文件
		/// </summary>
		/// <param name="f">文件路径</param>
		/// <returns>success return true  fail and is false</returns>
		public static bool DeleteFile(string f){
			
			if(File.Exists(f)){
				File.Delete(f);
				return true;
			}
			return false;
		}
		/// <summary>
		/// 获取文件信息
		/// </summary>
		/// <param name="f">文件路径</param>
		/// <param name="t">指定类型 1:公司 2:文件版本 4:文件描述</param>
		/// <returns>文件详情</returns>
		public static string GetFileInfo(string f,byte t){
			string s="NULL";
			if(File.Exists(f)==false){
				return s;
				
			}
			
			FileVersionInfo fi=FileVersionInfo.GetVersionInfo(f);
			switch(t){
				case 0:
					s=fi.ProductName;
					break;
				case 1:
					s=fi.CompanyName;
					break;
				case 2:
					s=fi.FileVersion;
					break;
				case 3:
					s=fi.ProductVersion;
					break;
				/*case 4:
					s=fi.ProductMajorPart.ToString+'.'+fi.ProductMinorPart.ToString+'.'+fi.ProductBuildPart.ToString+'.'+fi.ProductPrivatePart.ToString;
					break;*/
				case 4:
					s=fi.FileDescription;
					break;
			}
			if(s==null | s==""){
				return "";
			}else{
				return s;
			}
		}
		public static void ShowFileLocation(string f){
			ProcessStartInfo psi=new ProcessStartInfo("explorer.exe");
			psi.Arguments="/e,/select,"+f;
			Process.Start(psi);
		}
		//argument error
		public static bool ShowPropertyFile(string f,IntPtr hwnd){
			SHELLEXECUTEINFO info=new SHELLEXECUTEINFO();
			info.cbSize=Marshal.SizeOf(info);
			info.lpVerb="Properties";
			info.lpFile=f;
			info.nShow=SW_SHOW;
			info.fMask=SEE_MASK_INVOKEIDLIST |SEE_MASK_FLAG_NO_UI|SEE_MASK_NOCLOSEPROCESS;
			info.hwnd=hwnd;
			info.nShow=5;
			info.hInstApp=IntPtr.Zero;
			info.lpIDList=IntPtr.Zero;
			info.lpParameters=null;
			info.lpDirectory="c:\\";
			return ShellExecuteEx(ref info);
		}
		public static bool Shutdown(uint uFlag){
			bool ret=ExitWindowsEx(uFlag,0);
			return ret;
		}
		public static void PowerShutdown(int action){
			NtShutdownSystem(action);
		}
		
	}
	class reg{
		string rkey="HKEY_CURRENT_USER\\Software\\";
			
	}
}
